package com.lfnyxy.common;

import org.apache.log4j.Logger;

import java.text.MessageFormat;

/**
 * @Author:XXX
 * @Description:
 * @Created by wanghaisheng on 2017/9/24.
 * Modified By:
 */
public class LogTool {
    private static Logger logger = Logger.getLogger(LogTool.class);



    private static final String Log_Code_Msg = "Code: {0}, Content: {1}";

    private static final String Log_Msg = "Content: {1}";

    public static void log(String message){
        log(null,message);
    }

    public static void log(String code,String message){
        if (StringTool.isNotEmpty(message)){
            if (StringTool.isNotEmpty(code)){
                logger.error(MessageFormat.format(Log_Code_Msg, code , message));
            }else{
                logger.error(MessageFormat.format(Log_Msg, message));
            }
        }
    }

    public static void WriteError(String code, String message){
        if(StringTool.isNotEmpty(message)){
            if(StringTool.isNotEmpty(code)){
                code = "未定义错误编码";
            }

            String content = DateTool.getCurrentData(StringTool.Empty()) + ", code:"+  code + ", content:"+ message ;
            FileTool.WriteToFile(FileTool.GetRootPath()+getFileName(true), content, true);
        }
    }

    private static String getFileName(boolean error){
        if(error){
            //错误文件一个。不重写。
            return "errLog";
        }else{
            return "infoLog" + DateTool.getCurrentData(StringTool.Empty());
        }
    }
}
